<?php
class ClientsPlanesController extends AppController {

	var $name = 'ClientsPlanes';
	var $uses = array('ClientsPlan','Client','Plan','Money','ClientsPerson');

    function beforeFilter () {
        parent::beforeFilter();
    }	
    
	function index() {
        $this->set('title_nav_bar','Clientes / Planes','Clientes / Planes');
		if ( isset( $_POST['filtro'] ) ) { 
			$filtro=$_POST['filtro']; 
		} else { 
			if (isset($_SESSION['filtro']) ) {
				$filtro = $_SESSION['filtro'];	
			} else {
				$filtro='';	
			}
			
		}
		$_SESSION['filtro'] = $filtro;
		$this->set('filtro',$filtro);
		$this->set('clientes_planes', $this->paginate('ClientsPlan', array('Clients.name LIKE' => "%$filtro%")) );
	}

	function limpiar_filtro() {
		$_SESSION['filtro'] = '';
		$this -> redirect(array('action'=>'index'));
		exit();                
	}

    function edit($id=0) {

		if ( isset($_POST['save']) ) {
			$fecha = $this->data['ClientsPlan']['fecha_alta'];
			$fecha = substr($fecha,6,4)."-".substr($fecha,3,2).'-'.substr($fecha,0,2);
			$this->data['ClientsPlan']['fecha_alta'] = $fecha;
			
			if ($this->data['ClientsPlan']['id'] > 0 ) {
				$sql = "select count(id) as cuantos from clients_people where client_id='{$this->data['ClientsPlan']['client_id']}'";
				$x = $this->ClientsPerson->query($sql);
				$this->data['ClientsPlan']['pacientes_actuales'] = $x[0][0]['cuantos'];
			} else {
				$this->data['ClientsPlan']['pacientes_actuales'] = 0;
			}
			
			if ($this->ClientsPlan->save($this->data['ClientsPlan'])) {
				$this->Session->setFlash(__('Guardado correctamente', true));
				$this -> redirect(array('action'=>'index'));
				exit();                
			} else {
				$this->Session->setFlash(__('No se pudo guardar el formulario.', true));
			}  
			
		} 
            
        if ($id == 0) {
                $this->set('title_nav_bar','Crear Nueva Asociación Cliente/Plan');
                // Nuevo Plan
                $T = $this->ClientsPlan->_schema;
                foreach($T as $clave => $valor) { $this->data['ClientsPlan'][$clave] = $valor['default']; }
                $this->data['ClientsPlan']['id'] = 0;    
				
        } else {
                $this->set('title_nav_bar','Editar Cliente/Plan (id:'.$id.')');
				$this->ClientsPlan->recursive = 2;
                $this->data= $this->ClientsPlan->read(null, $id);
        }
		$clientes = $this->Client->find('list', array('fields' => array('Client.name')) );
		$this->set('clientes',$clientes);
		$planes = $this->Plan->find('list', array('fields' => array('Plan.name')) );
		$this->set('planes',$planes);
		
        
        
    }


	function delete() {

		if (empty($this->data['Delete'])) {
			$this->Session->setFlash(__('Debe seleccionar un plan', true));
			$this->redirect(array('action'=>'index'));
		} else {
 
            foreach($this->data['Delete'] as $key=>$value) {
                if ($value==1) { 
                    $this->Plan->delete($key,true); 
                }
            }
            $this->Session->setFlash(__('Plan/es eliminado/s', true));
    		$this->redirect(array('action'=>'index'));

		} 
	}

	
	function eliminarcampo($formulario_id=0,$campo_id=0 ) {

		if ($campo_id==0 OR $formulario_id==0) {
			$this->Session->setFlash(__('Debe seleccionar un campo del formulario', true));
			$this->redirect(array('action'=>'index'));
		} else {
 
            $this->Formularioscampo->delete($campo_id,true); 

            $this->Session->setFlash(__('Campo del Formulario eliminado', true));
    		$this->redirect(array('action'=>'edit/'.$formulario_id));

		} 
	}
	
	
}
